package jspservlet.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jspservlet.dao.UserDAO;
import jspservlet.dao.impl.UserDAOImpl;
import jspservlet.vo.User;

public class ModifyInfoServelt extends HttpServlet {
	
	public void doGet(HttpServletRequest req,HttpServletResponse res) 
		throws IOException,ServletException{
	}
	
	public void doPost(HttpServletRequest req,HttpServletResponse res) 
			throws IOException,ServletException{
		ServletContext application = this.getServletContext();
		HttpSession session=req.getSession();
		session.setAttribute("username", (String) application.getAttribute("username"));
		User user=new User();
		user.setUsername(application.getAttribute("username").toString());
		user.setGender(req.getParameter("gender"));
		user.setDob(req.getParameter("dofb"));
		user.setFamilyid(req.getParameter("fID"));
		user.setPassword(req.getParameter("address"));//把地址存在了密码里，没关系
		System.out.println(application.getAttribute("username").toString());
		UserDAO dao = new UserDAOImpl();
		int flag=0;
		try {
			flag = dao.modifyinfo(user,session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (flag==1) {
			res.getWriter().println("<script>alert('Successful Modification!')</script>");
			res.getWriter().println("<script>window.location.href='./ModifyInfo.jsp'</script>");
		}else {
			res.getWriter().println("<script>alert('Failed Modification! Please try again.')</script>");
			res.getWriter().println("<script>window.location.href='./ModifyInfo.jsp'</script>");
		}
	}
}
